#include "TBar.h"
#include "CCControlSlider.h"
#include "TUI.h"

TBar::TBar(){ 
    m_nPer=100;
    m_pItem=NULL;
}
bool TBar::init(){
    if(!TPanel::init()){
        return false;
    }
    return true;
}


TBar::~TBar(){
   
}



void TBar::setSource(const char* path){
  
    
    if(PACK_ASSETS_PLIST){
        
        if(!CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(path)){
            return;
        }
        
        if(m_pItem==NULL){
            m_pItem=CCSprite::createWithSpriteFrameName(path ); 
            addChild(m_pItem);
        }else{
            m_pItem->setDisplayFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(path));
        }
    }else{
        if(m_pItem==NULL){
            m_pItem=CCSprite::create(path);
            m_pItem->setCascadeOpacityEnabled(true);
            addChild(m_pItem);
        }else{
            m_pItem->initWithFile(path);
        }
        
    }
    
    
   
    
    int width=m_pItem->getContentSize().width;
    int height=m_pItem->getContentSize().height;
    
    m_pItem->setPositionX(width/2);
    m_pItem->setPositionY(-height/2);
    
    
    setContentSize(CCSizeMake(width, height));
}


TBar* TBar::create(xml_node<> * pItem){
    
    TBar*  ret=TBar::create();
    ret->initWidthConf(pItem);
    return ret;
}

void TBar::initWidthConf(xml_node<> * pItem){
    string path;
    readAttrString(pItem, "source", path);
    
    float per= readAttrFloat(pItem, "per");
    setPer(per);
    
    
    if(!PACK_ASSETS_PLIST){
        m_pItem=CCSprite::create(path.c_str());
    }else{ 
        m_pItem=CCSprite::createWithSpriteFrameName(path.c_str());
    }
    m_pItem->setPosition(ccp(m_pItem->getContentSize().width/2,-m_pItem->getContentSize().height/2));
    addChild(m_pItem);
    
    
    setContentSize(m_pItem->getContentSize());
}


 
void TBar::setNum(long lCur,long lMax){
    if(!getParent()){
        return;
    }
   
    
    setPer(lCur*1.0f/lMax);
}

void TBar::setPer(float num){
  
  
    m_nPer=num*100;
    
    if(m_nPer<=0){
        m_nPer=0;
    }
    
    if(m_nPer>100){
        m_nPer=100;
    }
}
void TBar::beforeDraw(){
    glEnable(GL_SCISSOR_TEST);
    
    if(this->getParent()){
        CCSize size=CCDirector::sharedDirector()->getWinSize();
        
        CCPoint worldPos= convertToWorldSpace(ccp(0 ,-getContentSize().height));
        
        if(worldPos.x<-getContentSize().width||
           worldPos.y<-getContentSize().height||
           worldPos.x>size.width||
           worldPos.y>size.height
           ){
            return;
        }
        CCEGLView::sharedOpenGLView()->setScissorInPoints(
                                                          worldPos.x,
                                                          worldPos.y,
                                                          getContentSize().width*(m_nPer/100.0f)*getScaleX(),
                                                          getContentSize().height
                                                          );
        
        
    }
    
}


void TBar::afterDraw()
{
    
    glDisable(GL_SCISSOR_TEST);
    
}

void TBar::visit()
{
	// quick return if not visible
	if (!isVisible())
    {
		return;
    }
    
	kmGLPushMatrix();
	
    //	glPushMatrix();
	
    if (m_pGrid && m_pGrid->isActive())
    {
        m_pGrid->beforeDraw();
        this->transformAncestors();
    }
    
	this->transform();
    this->beforeDraw();
    
	if(m_pChildren)
    {
		ccArray *arrayData = m_pChildren->data;
		unsigned int i=0;
		
		// draw children zOrder < 0
		for( ; i < arrayData->num; i++ )
        {
			CCNode *child =  (CCNode*)arrayData->arr[i];
			if ( child->getZOrder() < 0 )
            {
				child->visit();
			}
            else
            {
				break;
            }
		}
		
		// this draw
		this->draw();
		
		// draw children zOrder >= 0
		for( ; i < arrayData->num; i++ )
        {
			CCNode* child = (CCNode*)arrayData->arr[i];
			child->visit();
		}
        
	}
    else
    {
		this->draw();
    }
    
    this->afterDraw();
	if ( m_pGrid && m_pGrid->isActive())
    {
		m_pGrid->afterDraw(this);
    }
    
	kmGLPopMatrix();
    
}



/***************************************************
 * TEditBar
 **************************************************/
 

TEditBar::TEditBar(){
    controller=NULL;
    m_pMoveListener=NULL;
    m_pMovefnSelector=NULL;
}
TEditBar* TEditBar::create(xml_node<> * pItem){
    
    TEditBar*  ret=TEditBar::create();
    ret->initWidthConf(pItem);
    return ret;
}

void TEditBar::setChangeTarget(CCObject*  obj ,SEL_MenuHandler selector){
    m_pMoveListener=obj;
    m_pMovefnSelector=selector;
} 
void TEditBar::valueChanged(CCObject *sender, CCControlEvent controlEvent)
{
    if(m_pMoveListener&& m_pMovefnSelector)
        (m_pMoveListener->*m_pMovefnSelector)(this);
    
}

void TEditBar::initWidthConf(xml_node<> * pItem){
    TPanel::initWidthConf(pItem);
    string path,progress,thumb;
    readAttrString(pItem, "flag", thumb);
    readAttrString(pItem, "source", progress);
    readAttrString(pItem, "bg", path);
    controller=CCControlSlider::create(CCSprite::createWithSpriteFrameName(path.c_str()),
                                       CCSprite::createWithSpriteFrameName(progress.c_str()),
                                       CCSprite::createWithSpriteFrameName(thumb.c_str())
                                       );
    addChild(controller);
    controller->setTouchPriority(kCCMenuHandlerPriority);
    controller->setPositionX(getContentSize().width/2);
    controller->setPositionY(-getContentSize().height/2);
    
    controller->addTargetWithActionForControlEvents(this,
                                                    cccontrol_selector(TEditBar::valueChanged),
                                                    CCControlEventValueChanged);
    controller->setMaximumValue(100.0f);
    controller->setMinimumValue(0.0f);
    
    setPer(0.5);
}


void TEditBar::setPer(float num){
    if(controller)
    controller->setValue(num*100);
}


float TEditBar::getPer(){
    if(controller)
    return controller->getValue()/100.0f;
    
    return 0;
}
